#!/bin/bash
# Configuration Update Script
# This script updates Maven, Git, and Nginx configurations from the demo repository
# It should be run manually when needed, not automatically

# Determine workspace directory
if [ -z "$AWS" ]; then
    if [ -n "$HOME" ]; then
        export AWS="$HOME/workspace"
    else
        export AWS="/c/Users/$(whoami)/workspace"
    fi
fi

DEMO_ROOT="$AWS/demo"
USER_PROFILE="${HOME:-/c/Users/$(whoami)}"

echo "Configuration Update Script"
echo "=========================="
echo "Demo Root: $DEMO_ROOT"
echo "User Profile: $USER_PROFILE"
echo ""

# Function to update Maven configuration
update_maven_config() {
    echo "Updating Maven configuration..."
    
    local source_dir="$DEMO_ROOT/matrix_shell/config/apache_maven/hk"
    local target_dir="$USER_PROFILE/.m2"
    
    if [ ! -d "$source_dir" ]; then
        echo "  Error: Source directory not found: $source_dir"
        return 1
    fi
    
    # Create target directory if it doesn't exist
    mkdir -p "$target_dir"
    
    # Copy files using rsync if available, otherwise use cp
    if command -v rsync >/dev/null 2>&1; then
        rsync -av "$source_dir/" "$target_dir/"
    else
        cp -rf "$source_dir/"* "$target_dir/" 2>/dev/null || true
    fi
    
    echo "  Maven configuration updated successfully."
    echo ""
}

# Function to update Git configuration
update_git_config() {
    echo "Updating Git configuration..."
    
    local source_dir="$DEMO_ROOT/matrix_shell/config/git"
    local target_dir="$USER_PROFILE"
    
    if [ ! -d "$source_dir" ]; then
        echo "  Error: Source directory not found: $source_dir"
        return 1
    fi
    
    # Ensure target .ssh directory exists
    mkdir -p "$USER_PROFILE/.ssh"
    chmod 700 "$USER_PROFILE/.ssh" 2>/dev/null || true
    
    # Copy .gitconfig file
    if [ -f "$source_dir/.gitconfig" ]; then
        cp "$source_dir/.gitconfig" "$USER_PROFILE/.gitconfig"
        echo "  Copied .gitconfig file"
    fi
    
    # Copy SSH keys if they exist
    if [ -d "$source_dir/.ssh" ]; then
        cp -f "$source_dir/.ssh/id_rsa" "$USER_PROFILE/.ssh/" 2>/dev/null || true
        cp -f "$source_dir/.ssh/id_rsa.pub" "$USER_PROFILE/.ssh/" 2>/dev/null || true
        cp -f "$source_dir/.ssh/yyf.pem" "$USER_PROFILE/.ssh/" 2>/dev/null || true
        chmod 600 "$USER_PROFILE/.ssh/id_rsa" 2>/dev/null || true
        chmod 644 "$USER_PROFILE/.ssh/id_rsa.pub" 2>/dev/null || true
        chmod 600 "$USER_PROFILE/.ssh/yyf.pem" 2>/dev/null || true
        echo "  Copied SSH keys"
    fi
    
    echo "  Git configuration updated successfully."
    echo ""
}

# Function to update Nginx configuration
update_nginx_config() {
    echo "Updating Nginx configuration..."
    
    local source_dir="$DEMO_ROOT/matrix_shell/config/nginx"
    
    if [ ! -d "$source_dir" ]; then
        echo "  Error: Source directory not found: $source_dir"
        return 1
    fi
    
    # Detect OS and set appropriate paths and config file
    local nginx_conf=""
    local nginx_paths=()
    
    case "$(uname -s)" in
        MINGW*|MSYS*|CYGWIN*)
            # Windows
            nginx_conf="nginx_windows.conf"
            nginx_paths=(
                "/c/Program Files/nginx/conf"
                "/c/App/WWW_nginx/conf"
                "$USER_PROFILE/nginx/conf"
            )
            ;;
        Darwin)
            # Mac
            nginx_conf="nginx_mac.conf"
            nginx_paths=(
                "/opt/homebrew/etc/nginx"
                "/usr/local/etc/nginx"
                "$USER_PROFILE/nginx/conf"
            )
            ;;
        Linux)
            # Linux
            nginx_conf="nginx_linux.conf"
            nginx_paths=(
                "/usr/local/nginx/conf"
                "/etc/nginx"
                "$USER_PROFILE/nginx/conf"
            )
            ;;
        *)
            echo "  Warning: Unknown OS. Using default paths."
            nginx_conf="nginx.conf"
            nginx_paths=(
                "/c/App/WWW_nginx/conf"
                "$USER_PROFILE/nginx/conf"
            )
            ;;
    esac
    
    local nginx_conf_found=false
    local config_source_path="$source_dir/$nginx_conf"
    
    # Check if specific config file exists
    if [ ! -f "$config_source_path" ]; then
        echo "  Error: Nginx config file not found: $config_source_path"
        return 1
    fi
    
    # Try to find Nginx configuration directory
    for nginx_path in "${nginx_paths[@]}"; do
        if [ -d "$nginx_path" ]; then
            echo "  Found Nginx config directory: $nginx_path"
            
            # Copy configuration file with appropriate name
            if command -v rsync >/dev/null 2>&1; then
                rsync -av "$config_source_path" "$nginx_path/nginx.conf" 2>/dev/null || true
            else
                cp -f "$config_source_path" "$nginx_path/nginx.conf" 2>/dev/null || true
            fi
            
            nginx_conf_found=true
            break
        fi
    done
    
    if [ "$nginx_conf_found" = false ]; then
        echo "  Warning: Nginx configuration directory not found."
        echo "  Please manually copy Nginx configurations from: $source_dir"
        echo "  To your Nginx conf directory."
        echo ""
        return 1
    fi
    
    echo "  Nginx configuration updated successfully."
    echo ""
}

# Main execution
main() {
    echo "Starting configuration update process..."
    echo ""
    
    # Update configurations
    update_maven_config
    update_git_config
    update_nginx_config
    
    echo "Configuration update process completed."
    echo ""
    echo "Note: For Nginx configuration to take effect, you may need to restart Nginx service."
}

# Run main function if script is executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    main "$@"
fi